//
//  TranslateTextView.swift
//  SpeechAnalyzerDemo
//
//  Created by yly on 2025/10/27.
//

import UIKit
//import MLKitTranslate

//class TranslateTextView: UIView {
//    
//    // 创建英德互译器
//    let translator = Translator.translator(options: TranslatorOptions(sourceLanguage: .chinese, targetLanguage: .english))
//    
//    private let textView = UITextView()
//    private let placeholderLabel = UILabel()
//    private let confidenceContainer = UIStackView()
//    private let confidenceTitleLabel = UILabel()
//    private let confidenceValueLabel = UILabel()
//
//    override init(frame: CGRect) {
//        super.init(frame: frame)
//        setupLayout()
//        configureUI()
//    }
//
//    @available(*, unavailable)
//    required init?(coder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
//
//    func update(finalized: String, volatile: String, confidence: Double?) {
//        let combined = finalized + volatile
//        if combined.isEmpty {
//            placeholderLabel.isHidden = false
//            textView.isHidden = true
//        } else {
//            placeholderLabel.isHidden = true
//            textView.isHidden = false
//            textView.text = combined
//
//            translator.downloadModelIfNeeded {[weak self] in
//                if $0 == nil {
//                    //
//                    self?.translator.translate(combined) { [weak self] str , _ in
//                        self?.textView.text = str
//                    }
//                }
//            }
// 
//        }
//
//        if let confidence {
//            confidenceContainer.isHidden = false
//            confidenceValueLabel.text = String(format: "%.2f", confidence)
//        } else {
//            confidenceContainer.isHidden = true
//        }
//    }
//}
//
//// MARK: - 布局与样式
//private extension TranslateTextView {
//    
//    func setupLayout() {
//        translatesAutoresizingMaskIntoConstraints = false
//        layoutMargins = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)
//
//        let containerStack = UIStackView(arrangedSubviews: [placeholderLabel, textView, confidenceContainer])
//        containerStack.axis = .vertical
//        containerStack.spacing = 12
//        containerStack.translatesAutoresizingMaskIntoConstraints = false
//
//        addSubview(containerStack)
//
//        NSLayoutConstraint.activate([
//            containerStack.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
//            containerStack.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
//            containerStack.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
//            containerStack.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
//        ])
//    }
//
//    func configureUI() {
//        layer.borderColor = UIColor.separator.cgColor
//        layer.borderWidth = 1 / UIScreen.main.scale
//        layer.cornerRadius = 8
//
//        textView.isEditable = false
//        textView.isScrollEnabled = true
//        textView.font = .preferredFont(forTextStyle: .body)
//        textView.backgroundColor = .clear
//        textView.textContainerInset = .zero
//        textView.textContainer.lineFragmentPadding = 0
//        textView.heightAnchor.constraint(greaterThanOrEqualToConstant: 120).isActive = true
//
//        placeholderLabel.text = "No translate test."
//        placeholderLabel.font = .preferredFont(forTextStyle: .subheadline)
//        placeholderLabel.textColor = .secondaryLabel
//
//        confidenceContainer.axis = .horizontal
//        confidenceContainer.spacing = 8
//        confidenceContainer.isHidden = true
//
//        confidenceTitleLabel.text = "Confidence"
//        confidenceTitleLabel.font = .preferredFont(forTextStyle: .subheadline)
//        confidenceTitleLabel.textColor = .secondaryLabel
//
//        confidenceValueLabel.font = .preferredFont(forTextStyle: .subheadline)
//        confidenceValueLabel.textColor = .secondaryLabel
//
//        confidenceContainer.addArrangedSubview(confidenceTitleLabel)
//        confidenceContainer.addArrangedSubview(confidenceValueLabel)
//    }
//}
//
